package com.comleader.operator.service;

import static com.comleader.operator.pojo.entity.table.InspectRecordTableDef.INSPECT_RECORD;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.comleader.operator.common.enums.InspectStatusEnum;
import com.comleader.operator.common.page.PageQuery;
import com.comleader.operator.mapper.InspectRecordMapper;
import com.comleader.operator.pojo.entity.InspectRecordEntity;
import com.comleader.operator.pojo.param.InspectRecordParam;
import com.comleader.operator.pojo.param.InspectReportParam;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 检查记录表 服务层实现。
 *
 * @author 连新景
 * @since 1.0
 */
@Slf4j
@Service
public class InspectRecordService extends ServiceImpl<InspectRecordMapper, InspectRecordEntity> {

    public Page<InspectRecordEntity> selectPage(InspectRecordParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(INSPECT_RECORD).where(INSPECT_RECORD.OPERATOR_NAME.like(param.getOperatorName()))
            .and(INSPECT_RECORD.TYPE.eq(param.getType())).orderBy(INSPECT_RECORD.ID.desc());
        return page(PageQuery.build(param), queryWrapper);
    }



    /**
     * @Description 更新状态和完成时间
     * @Auhtor 连新景
     * @Date 2024/08/08 15:41
     * @param id
     * @param inspectStatusEnum
     * @return int
     */
    public int updateStatusAndFinishTime(Integer id, InspectStatusEnum inspectStatusEnum) {
        InspectRecordEntity inspectRecord = UpdateEntity.of(InspectRecordEntity.class, id);
        inspectRecord.setInspectStatus(inspectStatusEnum.getStatus());
        inspectRecord.setInspectFinishTime(LocalDateTime.now());
        return getMapper().update(inspectRecord);
    }

    public void download(InspectReportParam param, HttpServletResponse response) throws IOException {
        InspectRecordEntity inspectRecord = getById(param.getRecordId());
        String reportFileDir = inspectRecord.getReportFileDir();
        String zipName = inspectRecord.getOperatorName() + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) + "_检查报告.zip";
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(zipName, "UTF-8"));
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
            for (String fileName : param.getFileNameList()) {
                File file = new File(reportFileDir + fileName);
                ZipEntry zipEntry = new ZipEntry(fileName);
                zipOutputStream.putNextEntry(zipEntry);
                try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = bis.read(buffer)) != -1) {
                        zipOutputStream.write(buffer, 0, bytesRead);
                    }
                }
                zipOutputStream.closeEntry();
            }
        } catch (IOException e) {
            log.error("下载报告异常", e);
            throw e;
        }

    }

}